//电梯调度系统
//运行环境：http://play.elevatorsaga.com/
//源码下载地址：https://github.com/magwo/elevatorsaga
//在index.html的最后调用此脚本替换原来的函数
"use strict"
var system={
    init: function(elevators, floors) {

        //分配系统:以elevators数组作为分配系统
        elevators.invoke=function(floorNum){
            // console.log("elevators.invoke:"+floorNum);
            // var minData=null;
            // for(var i=0;i<this.length;i++){
            //     var data=elevators[i].invokeData(floorNum);
            //     if(minData==null){
            //         minData=data;
            //     }
            //     else{
            //         if(data.distance<minData.distance){
            //             minData=data;
            //         }
            //     }
            // }
            // this[minData.elevatorNum].addDestination(floorNum);
            this[0].addDestination(floorNum);
        }
        
        //楼层
        //每层增加三个属性up,down分别表示这一层要往上走，要往下走的人数
        floors.forEach(function (floor,floorNum){
            floor.on("up_button_pressed", function() {
                var elevatorNum=elevators.invoke(floorNum);
            })
            floor.on("down_button_pressed", function() {
                var elevatorNum=elevators.invoke(floorNum);
            })
        });

        //电梯
        elevators.forEach(function(elevator,index){
            //返回调度电梯的参考数据
            elevator.invokeData=function(floorNum){
                var direction=this.destinationDirection();
                var pressed=this.getPressedFloors();
                var current=this.currentFloor();
                var data={
                    "elevatorNum":index,
                    "distance":null,
                    "stopTime":null
                }
                var distance=floorNum-current;
                if(this.isUp){
                    if(distance>=0){
                        data.distance=distance;
                    }
                    else{
                        data.distance=current+floorNum;
                    }
                }
                else{
                    if(distance>=0){
                        data.distance=2*floors.length-current-floorNum;
                    }
                    else{
                        data.distance=-distance
                    }
                }
                return data;
            }
            //电梯是否正在向上运行
            elevator.isUp=true;
            elevator.run=function() {
                console.log("elevator.run");
                // console.group();
                var current=this.currentFloor();
                var goto=null;
                if(this.isUp){
                    goto=this.nextDestination(current);
                }
                else{
                    goto=this.prevDestination(current);
                }
                if(goto!==false){
                    //this.destinationQueue=[];
                    this.goToFloor(goto);
                }
                // console.log("电梯正在向" + (this.isUp?"上":"下") + "运行");
                // console.log("当前在第"+current+"层");
                // console.log("当前任务序列:"+this.destinations);
                // console.log(goto===false?"没有下一站,切换运行方向":("下一站是第"+goto+"层"));
                this.isUp=!this.isUp;
                if(this.isUp){
                    goto=this.nextDestination(current);
                }
                else{
                    goto=this.prevDestination(current);
                }
                // console.log("电梯正在向" + (this.isUp?"上":"下") + "运行");
                // console.log(goto===false?"没有下一站,停止运行":("下一站是第"+goto+"层"));
                if(goto!==false){
                    //this.destinationQueue=[];
                    this.goToFloor(goto);
                }
                console.log("elevator.run end "+goto);
                // console.groupEnd();
            }
            
            elevator.destinations=[];
            elevator.addDestination=function(floorNum){
                if(this.destinations.length===0){
                    this.destinations.push(floorNum);
                }
                else if(this.destinations[this.destinations.length-1]<floorNum){
                    this.destinations.push(floorNum);
                }
                else if(this.destinations[0]>floorNum){
                    this.destinations.splice(0,0,floorNum);
                }
                else{
                    for(var i=0;i<this.destinations.length-1;i++){
                        if(floorNum>this.destinations[i]&&floorNum<this.destinations[i+1]){
                            this.destinations.splice(i,0,floorNum);
                        }
                    }
                }
            }
            elevator.removeDestination=function(floorNum){
                for(var i=0;i<this.destinations.length;i++){
                    if(this.destinations[i]===floorNum){
                        this.destinations.splice(i,1);
                    }
                }
            }
            elevator.nextDestination=function(current){
                for(var i=0;i<this.destinations.length;i++){
                    if(this.destinations[i]>current){
                        return this.destinations[i];
                    }
                }
                return false;
            }
            elevator.prevDestination=function(current){
                for(var i=this.destinations.length-1;i>=0;i--){
                    if(this.destinations[i]<current){
                        return this.destinations[i];
                    }
                }
                return false;
            }
            elevator.on("idle", function() {
                this.goToFloor(0);
                this.goToFloor(floors.length);
            });
            elevator.on("floor_button_pressed", function(floorNum) {
            });
            elevator.on("passing_floor", function(floorNum, direction) {
                if(this.buttonStates[floorNum]){
                    this.goToFloor(floorNum,true);
                }
            });
            elevator.on("stopped_at_floor", function(floorNum) {
            });
        });
    },
    update: function(dt, elevators, floors) {
        // We normally don't need to do anything here
    }
}
var getCodeObjFromCode = function(code) {
    return system;
}